<template>
  <div class="wrapper">
    <div class="left">
      <div class="left-pic" />
    </div>
    <div class="right" >
      <div class="title" v-if="er">{{tit}}</div>
<!--      <div style="width: 380px; height: 410px; border: 1px solid red" ><span id="login_container"></span></div>-->
      <el-form ref="loginForm"
               :model="loginForm"
               :rules="loginRules"
               class="login-form"
               auto-complete="on"
               label-position="left" v-if="er2">
        <div class="title">{{name}}</div>
        <el-form-item prop="username">
          <el-input ref="name"
                    v-model="loginForm.username"
                    autofocus="autofocus"
                    name="username"
                    type="text"
                    auto-complete="on"
                    placeholder="请输入用户名"
                    @keyup.enter.native="handleLogin" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password"
                    v-model="loginForm.password"
                    name="password"
                    auto-complete="on"
                    placeholder="请输入密码"
                    @keyup.enter.native="handleLogin" />
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading"
                     @click.native.prevent="handleLogin"
                     class="submit-btn">
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div><div @click="sao" id="sao" style="float: left;"  v-bind:class="{changeColor:er}" > </div></div>
      <div>
        <el-dropdown @command="handleCommand">
          <el-button type="primary">
            第三方登录<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="a">钉钉扫码登录</el-dropdown-item>
            <el-dropdown-item command="b">钉钉账号登录</el-dropdown-item>
            <el-dropdown-item command="c">微信企业号登陆</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div class="loadEffect" v-if="er3">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
      </div>
      <div class="copyright">
<!--        悟空CRM受国家计算机软件著作权保护，未经授权不得进行商业行为，违者必究。<br>-->
<!--        ©2019 悟空软件<a target="_blank" href="http://www.5kcrm.com">www.5kcrm.com</a>-->
      </div>
    </div>

    <img class="logo"
         :src="logo" />
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import { WXden } from '@/api/login'
import { mapGetters } from 'vuex'
import Lockr from 'lockr'


export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length == 0) {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          {required: true, trigger: 'blur', validator: validateUsername}
        ],
        password: [{required: true, trigger: 'blur', validator: validatePass}]
      },
      loading: false,
      redirect: undefined,
      remember: false,
      er: false,
      er2: true,
      er3: false,
      tit: "钉钉扫描二维码登录",
      code: '',
      urls: ''
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
        if (getQueryVariable("code") != "") {
          var parms = {};
          parms["username"] = getQueryVariable("code");
          this.$store.dispatch('dingLogin', parms).then(res => {
            this.er2 = false;
            this.er3 = true;
            this.$store.dispatch('SystemLogoAndName')
            this.$router.push({path: this.redirect || '/workbench/index'})
          }).catch(() => {
            this.loading = false
          })
        }
        if (getQueryVariable("token") != "") {
          var parms = {};
          parms["username"] = getQueryVariable("token");
          this.$store.dispatch('wxLogin', parms).then(res => {
            this.er2 = false;
            this.er3 = true;
            this.$store.dispatch('SystemLogoAndName')
            this.$router.push({path: this.redirect || '/workbench/index'})
          })
        }
      },
        // console.log("route"+this.$route.query.code
        //   if (getQueryVariable("token") != "") {
        //     var parms = {};
        //     parms["username"] = getQueryVariable("token");
        //     this.$store.dispatch('wxLogin', parms).then(res => {
        //       this.er2 = false;
        //       this.er3 = true;
        //       this.$store.dispatch('SystemLogoAndName')
        //       this.$router.push({path: this.redirect || '/workbench/index'})
        //     }).catch(() => {
        //       this.loading = false
        //     })
        //   }
        //   // console.log("code"+getQueryVariable("code"))
        // }
        immediate: true
      }
    },
    computed: {
      ...mapGetters(['logo', 'name'])
    },
    mounted() {
      /*var vm = window.app*/
    },
    methods: {
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            debugger
            this.$store
              .dispatch('Login', this.loginForm)
              .then(res => {
                this.loading = false
                this.$store.dispatch('SystemLogoAndName')
                this.$router.push({path: this.redirect || '/workbench/index'})
                // console.log("url"+this.$router.);
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            return false
          }
        })
      },
      handleCommand(command) {
        // this.$message('click on item ' + command);
        if (command == 'a') {
          this.sao();
        }
        if (command == 'b') {
          this.den();
        }
        if (command == 'c') {
          this.WXden();
        }
      },
      sao: function () {
        this.er = true;
        this.er2 = false;
        var url = encodeURIComponent("http://2a433746q0.wicp.vip/workbench/index");
        var goto = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingoahdwfojjd9iwfhirf&response_type=code&scope=snsapi_login&redirect_uri=' + url);
        var obj = DDLogin({
          id: "sao",//这里需要你在自己的页面定义一个HTML标签并设置id，例如<div id="login_container"></div>或<span id="login_container"></span>
          goto: goto, //请参考注释里的方式
          style: "border:none;background-color:#FFFFFF;",
          width: "365",
          height: "400"
        });
        var hanndleMessage = function (event) {
          var origin = event.origin;
          console.log("origin", event.origin);
          if (origin == "https://login.dingtalk.com") { //判断是否来自ddLogin扫码事件。
            var loginTmpCode = event.data; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
            console.log("loginTmpCode", loginTmpCode);
            this.loginTmpCode = loginTmpCode;
            this.goto = goto;
            var parm = {};
            parm["username"] = loginTmpCode;
            parm["password"] = goto;
            this.app.$store.dispatch('saoma', parm)
              .then(res => {
                this.app.$store.dispatch('SystemLogoAndName')
                this.app.$router.push({path: this.redirect || '/workbench/index'})
              }).catch(() => {
              this.loading = false
            })
          }
        }
        if (typeof window.addEventListener != 'undefined') {
          window.addEventListener('message', hanndleMessage, false);
        } else if (typeof window.attachEvent != 'undefined') {
          window.attachEvent('onmessage', hanndleMessage);
        }
      },
      den: function () {
        this.er = true;
        this.er2 = true;
        var url = encodeURIComponent('http://2a433746q0.wicp.vip/workbench/index');
        window.location.href = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingoahdwfojjd9iwfhirf&response_type=code&scope=snsapi_login&redirect_uri=" + url;
      },
      WXden: function () {
        // var urls = encodeURIComponent('http://2a433746q0.wicp.vip/workbench/index');
        WXden().then(data => {
          console.log("date" + data.url);
          window.location.href = data.url;
        }).catch(error => {
          reject(error)
        })
      }
    }
}
function getQueryVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if(pair[0] == variable){return pair[1];}
  }
  return(false);
}

</script>

<style lang="scss" scoped>
$dark_gray: #ccc;
$light_gray: #333;
$login_theme: #00aaee;

.changeColor{
  margin-right: 10%;
  padding-left: 5px;
}
/deep/ input {
  border: 0 none;
  background-color: white;
  -webkit-appearance: none;
  &:-webkit-autofill {
    background-image: none;
    -webkit-box-shadow: 0 0 0 1000px white inset !important;
    -webkit-text-fill-color: $light_gray !important;
  }
}
/deep/ .el-input__inner {
  height: 40px;
  padding: 0 12px;
  background-color: white;
  border: 0 none;
  border-bottom: 1px solid #e6e6e6 !important;
}
/deep/ .el-form-item__error {
  left: 12px;
}
.wrapper {
  position: relative;
  width: 100%;
  min-width: 1300px;
  display: flex;
  .left {
    width: 68%;
    .left-pic {
      width: 100%;
      height: 100%;
      background: url('../../assets/img/login/login.png') no-repeat center;
      background-size: cover;
    }
  }
  .right {
    position: relative;
    width: 32%;
    background-color: #fff;
    display: flex;
    align-items: center;
    flex-direction: column;
    padding-top: 6%;
    .el-form {
      width: 70%;
      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0 auto 50px;
        text-align: center;
      }
      .submit-btn {
        width: 100%;
        line-height: 2;
        font-size: 16px;
        color: white;
        border-radius: 3px;
        background-color: $login_theme;
        display: block;
      }
      .el-button {
        border: 0 none;
      }
      .action-control {
        color: #999;
        /deep/ .el-checkbox {
          .el-checkbox__label {
            color: #999;
          }
          .el-checkbox__input.is-checked .el-checkbox__inner {
            background-color: $login_theme;
            border-color: $login_theme;
          }
        }

        .forget {
          cursor: pointer;
          float: right;
        }
      }
    }

    .register {
      width: 70%;
      padding-top: 30px;
      color: $light_gray;
      border-top: 1px solid #e6e6e6;
      text-align: center;
      margin-top: 28px;
      .register-btn {
        color: $login_theme;
        cursor: pointer;
      }
    }

    .copyright {
      width: 92%;
      position: absolute;
      bottom: 2%;
      color: #d0d0d0;
      font-size: 12px;
      text-align: center;
      line-height: 1.5;
    }
  }

  .logo {
    position: absolute;
    left: 60px;
    top: 50px;
    width: 180px;
    height: 48px;
    z-index: 200;
  }
}
.loadEffect{
  width: 100px;
  height: 100px;
  position: relative;
  margin: 0 auto;
  margin-top:100px;
}
.loadEffect span{
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: lightgreen;
  position: absolute;
  -webkit-animation: load 1.04s ease infinite;
}
@-webkit-keyframes load{
  0%{
    opacity: 1;
  }
  100%{
    opacity: 0.2;
  }
}
.loadEffect span:nth-child(1){
  left: 0;
  top: 50%;
  margin-top:-8px;
  -webkit-animation-delay:0.13s;
}
.loadEffect span:nth-child(2){
  left: 14px;
  top: 14px;
  -webkit-animation-delay:0.26s;
}
.loadEffect span:nth-child(3){
  left: 50%;
  top: 0;
  margin-left: -8px;
  -webkit-animation-delay:0.39s;
}
.loadEffect span:nth-child(4){
  top: 14px;
  right:14px;
  -webkit-animation-delay:0.52s;
}
.loadEffect span:nth-child(5){
  right: 0;
  top: 50%;
  margin-top:-8px;
  -webkit-animation-delay:0.65s;
}
.loadEffect span:nth-child(6){
  right: 14px;
  bottom:14px;
  -webkit-animation-delay:0.78s;
}
.loadEffect span:nth-child(7){
  bottom: 0;
  left: 50%;
  margin-left: -8px;
  -webkit-animation-delay:0.91s;
}
.loadEffect span:nth-child(8){
  bottom: 14px;
  left: 14px;
  -webkit-animation-delay:1.04s;
}
</style>
